<template>
  <div class="container">
    <h1>新建住户</h1>
    <hr style="border: 1px solid rgb(240, 240, 240)" />
    <span>基础信息</span>
    <div class="body">
      <div class="input">
        <div class="input-left">
          <span>关联房屋</span>
        </div>
        <div class="input-right">
          <el-cascader
            v-model="connection_room"
            :options="options1"
            style="width: 300px"
            placeholder="请选择房屋"
            @change="handleChange"
          />
        </div>
      </div>
      <div class="input">
        <div class="input-left">
          <span>住户姓名</span>
        </div>
        <div class="input-right">
          <el-input v-model="hold_name" placeholder="请输入" />
        </div>
      </div>
      <div class="input">
        <div class="input-left">
          <span>住户编号</span>
        </div>
        <div class="input-right">
          <el-input v-model="hold_id" type="number" placeholder="请输入" />
        </div>
      </div>
      <div class="input">
        <div class="input-left">
          <span>住户性别</span>
        </div>
        <div class="input-right">
          <el-radio-group v-model="hold_sex">
            <el-radio value="男">男</el-radio>
            <el-radio value="女">女</el-radio>
          </el-radio-group>
        </div>
      </div>
      <div class="input">
        <div class="input-left">
          <span>证件类型</span>
        </div>
        <div class="input-right">
          <el-select v-model="certificate_type" placeholder="请选择">
            <el-option
              v-for="item in options2"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </div>
      </div>
      <div class="input">
        <div class="input-left">
          <span>证件号码</span>
        </div>
        <div class="input-right">
          <el-input
            v-model="certificate_num"
            type="number"
            placeholder="请输入"
          />
        </div>
      </div>
      <div class="input">
        <div class="input-left">
          <span>与业主关系</span>
        </div>
        <div class="input-right">
          <el-select v-model="relation_to_hold" placeholder="请选择">
            <el-option
              v-for="item in options3"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </div>
      </div>
      <div class="input">
        <div class="input-left">
          <span>手机号码</span>
        </div>
        <div class="input-right">
          <el-input v-model="phone_num" type="number" placeholder="请输入" />
        </div>
      </div>

      <div class="input">
        <div class="input-left">
          <span>备注</span>
        </div>
        <div class="input-right">
          <el-input v-model="remark" :rows="5" type="textarea" />
        </div>
      </div>
      <div style="margin: 2rem 0 2rem 12rem">
        <el-button type="primary" @click="addOrUpdate">提交</el-button>
        <el-button type="default" @click="to('file/household')">返回</el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { onMounted, ref } from "vue";
import to from "@/utils/to.js";
import axios from "axios";
import { ElMessage } from "element-plus";
import { useRoute } from "vue-router";

const route = useRoute();
const id = route.query.id;

const connection_room = ref("");
const hold_name = ref("");
const hold_id = ref("");
const hold_sex = ref("");
const certificate_type = ref("");
const certificate_num = ref("");
const relation_to_hold = ref("");
const phone_num = ref("");
const remark = ref("");
const floorData = ref([]);

const options1 = ref([]);

const options2 = [
  {
    value: "身份证",
    label: "身份证",
  },
  {
    value: "港澳通行证",
    label: "港澳通行证",
  },
  {
    value: "护照",
    label: "护照",
  },
  {
    value: "其他",
    label: "其他",
  },
];

const options3 = [
  {
    value: "本人",
    label: "本人",
  },
  {
    value: "亲属",
    label: "亲属",
  },
  {
    value: "朋友",
    label: "朋友",
  },
  {
    value: "租户",
    label: "租户",
  },
  {
    value: "其他",
    label: "其他",
  },
];

const getFloor = () => {
  const config = {
    url: "http://localhost:8080/floor/get",
    method: "GET",
    headers: {
      Authorization: localStorage.getItem("token"),
    },
  };

  const response = axios(config);
  response
    .then((data) => {
      floorData.value = data.data.data;
      options1.value = floorData.value.map((item) => ({
        value: item.name,
        label: item.name,
        children: Array.from({ length: item.unitNum }, (_, index) => ({
          label: `${index + 1}单元`,
          value: `${index + 1}单元`,
          children: Array.from({ length: 5 }, (_, j) => ({
            label: `${index + 1}0${j + 1}`,
            value: `${index + 1}0${j + 1}`,
          })),
        })),
      }));
    })
    .catch((error) => {
      console.log(error);
    });
};

const addOrUpdate = () => {
  let config = {};

  if (id === undefined || id === "") {
    config = {
      url: "http://localhost:8080/person/add",
      method: "POST",
      headers: {
        Authorization: localStorage.getItem("token"),
      },
      data: {
        houseDetail: Object.values(connection_room.value).join(","),
        name: hold_name.value,
        perNum: hold_id.value,
        sex: hold_sex.value,
        cardType: certificate_type.value,
        cardNum: certificate_num.value,
        relManager: relation_to_hold.value,
        phone: phone_num.value,
        frontImg: "",
        backImg: "",
        detail: remark.value,
      },
    };
  } else {
    config = {
      url: "http://localhost:8080/person/update",
      method: "PUT",
      headers: {
        Authorization: localStorage.getItem("token"),
      },
      data: {
        id: id,
        houseDetail: Object.values(connection_room.value).join(","),
        name: hold_name.value,
        perNum: hold_id.value,
        sex: hold_sex.value,
        cardType: certificate_type.value,
        cardNum: certificate_num.value,
        relManager: relation_to_hold.value,
        phone: phone_num.value,
        frontImg: "",
        backImg: "",
        detail: remark.value,
      },
    };
  }

  const response = axios(config);
  response
    .then((data) => {
      console.log(data);
      if (data.data.code === 1) {
        ElMessage({
          message: data.data.msg,
          type: "success",
        });
        setTimeout(() => {
          to("file/household");
        }, 2000);
      }
    })
    .catch((error) => {
      console.log(error);
    });
};

const get = () => {
  const config = {
    url: "http://localhost:8080/person/selectById",
    method: "GET",
    headers: {
      Authorization: localStorage.getItem("token"),
    },
    params: {
      id: id,
    },
  };

  const response = axios(config);
  response
    .then((data) => {
      connection_room.value = data.data.data.houseDetail.split(",");
      hold_name.value = data.data.data.name;
      hold_id.value = data.data.data.id;
      hold_sex.value = data.data.data.sex;
      certificate_type.value = data.data.data.cardType;
      certificate_num.value = data.data.data.cardNum;
      relation_to_hold.value = data.data.data.relManager;
      phone_num.value = data.data.data.phone;
      remark.value = data.data.data.detail;
    })
    .catch((error) => {
      console.log(error);
    });
};

onMounted(() => {
  get();
  getFloor();
});
</script>

<style scoped>
.container {
  background-color: white;
  height: auto;
  margin-top: 2rem;
  margin-bottom: 2rem;
}

h1 {
  font-size: 2rem;
  font-weight: 600;
  padding: 2rem 0 10px 4rem;
}

span {
  display: inline-block;
  font-size: 15px;
  margin: 1rem 2rem 1rem 0;
  line-height: 24px;
  width: 100px;
  text-align: right;
}

.body {
  display: grid;
  justify-content: center;
  align-items: center;
}

.input {
  display: flex;
  justify-content: center;
  align-items: center;
  width: auto;
}

.input-right {
  width: 300px;
}
</style>
